package com.sinog.front.controller.basic;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.basic.PositionService;
import com.sinog.front.service.user.UserFacade;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @desc 系统管理 - 岗位管理 - 岗位管理 - PositionController
 * @author -
 * @date -
 */
@RestController
public class PositionController {

    @Resource
    private PositionService service;
    @Resource
    private UserFacade userFacade;

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 条件获取岗位列表
     * @param request request
     * @param key key
     * @return String
     */
    @GetMapping("/basic/position/getPositionList")
    public String getPositionList(HttpServletRequest request,
                                  @RequestParam(required = false, defaultValue = "-1") String key) {
        Integer pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        UserDTO authentication = userFacade.getAuthentication();
        if(Constants.RoleId.ROLE_ID_AB.equals(authentication.getRoleid())) {
            return service.selectPositionList(key,pageIndex,pageSize,authentication.getLevel());
        }
        return ReturnCode.NO_PERMISSION.message();
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 保存岗位信息
     * @param request request
     * @param data data
     * @return Result
     */
    @Log(type = "更新", value = "保存岗位信息信息 参数中有id为【更新】 没有id为【新增】")
    @PostMapping("/basic/position/save")
    public Result savePosition(HttpServletRequest request,String data) {
        String roleid = userFacade.getAuthentication().getRoleid();
        if(Constants.RoleId.ROLE_ID_AB.equals(roleid)) {
            return service.save(data);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 根据ID查询岗位详情
     * @param id id
     * @return Result
     */
    @GetMapping("basic/position/{id}")
    public Result selectPosition(@PathVariable String id) {
        return service.selectOne(id);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 删除资源
     * @param request request
     * @param id id
     * @return Result
     */
    @Log(type = "删除", value = "删除岗位信息")
    @DeleteMapping("/basic/position/delete/{id}")
    public Result delPosition(HttpServletRequest request,@PathVariable String id) {
        String roleid = userFacade.getAuthentication().getRoleid();
        if(Constants.RoleId.ROLE_ID_AB.equals(roleid)) {
            return service.deleteOne(id);
        }
        return Result.failure(ReturnCode.NO_PERMISSION);
    }

    /**
     * 系统管理 - 岗位管理 - 岗位管理 - 获取全部岗位
     * 使用者: user_edit 页面回显
     * @param reskey reskey
     * @return String
     */
    @GetMapping("/basic/userPositionNameEdit/{reskey}")
    public String selectAllPosition(@PathVariable String reskey) {
        UserDTO loginUserInfo = userFacade.getAuthentication();
        Result result = service.selectAllPosition(reskey,loginUserInfo.getLevel());
        String json = "";
        if(Constants.Number.NUM_ONE.equals(result.getCode())) {
            json = JsonUtil.toText(result.getData());
        }
        return json;
    }
}